<?php

class FormTemplate extends Application {

//FORM NAME fId harus selalu ada karena dijadikan acuan dalam berbagai hal (dapat berperan sebagai PRIMKEY VALUE)
    private $mInputObject = array();
    private $mType = array('text', 'select');
    private $mFormMulti = false; // FormMulti adalah untuk input lebih dari satu form
    private $mFormTitle;
    private $mFormName;
    private $mFormId;
    private $mFormClass;
    private $mFormMethod;
    private $mFormAction;
    private $mFormTargetBack;
    private $mFormBackId;
    private $mFormOnSubmit;
    private $mFormIsSubmitButton = true;
    private $mFormInputPointer;
    private $mAction;
    private $mCurrentPKValue;
    private $mFieldSupport = array();
    private $mHiddentFieldCount = 0; //var untuk menampung jml hidden field yang ada, untuk keperluan field indexing (keyboard navigation), karena hidden field tidak termasuk
    private $mVisibilityForm = true;
    private $mErrorHandlerMessage;

    /*
     * * $this->mForm[$tIndex]['fieldName']
     * * $this->mForm[$tIndex]['inputName']
     * * $this->mForm[$tIndex]['inputType']

     */

    public function __construct($pParams = null) {
        $this->mFormName = $pParams['FormName'];
        (!empty($pParams['FormMethod'])) ? $this->mFormMethod = $pParams['FormMethod'] : $this->mFormMethod = 'get';
        $this->mFormAction = $pParams['FormAction'];
    }

    public function CalibrateInputForm() {
        $i = 0;
        $tTemp = array();
        foreach ($this->mInputObject as $tRes) {
            $tTemp[$i] = $tRes;
            $i++;
        }

//PERIKSA apakah ada hasil dari calibrating, jika ya eksekusi
        if ($tTemp) {
//RESET DATA FIELD
            $this->mInputObject = $tTemp;
        }
    }

    private function GetInputObjectIndex() {
        $this->CalibrateInputForm();
        $tCount = count($this->mInputObject);
        (!empty($tCount)) ? $tIndex = $tCount : $tIndex = 0;

        return $tIndex;
    }

    public function CreateInputForm() { //COUNSTRUCT INPUT FORM
        $tIndex = $this->GetInputObjectIndex();

//CHECK: JIKA nilai pointer belum memiliki nilai
        if (!empty($this->mFieldPointer)) {
//Karena nilai pointer sudah ada, maka buat yang baru
            $tIndex++;
        }

        $this->mFormInputPointer = $tIndex;
    }

    public function SetAction($pValue) {
        $this->mAction = $pValue;
    }

    public function SetFormName($pName) {
        $this->mFormName = $pName;

        if (empty($this->mFormId)) {
            $this->mFormId = $pName;
        }
    }

    public function SetFormClass($pValue) {
        $this->mFormClass = $pValue;
    }

    public function SetFormTitle($pValue) {
        $this->mFormTitle = $pValue;
    }

    public function SetFormTargetAction($pAction) {
        $this->mFormAction = $pAction;
    }

    public function SetFormTargetBack($pAction) {
        $this->mFormTargetBack = $pAction;
    }

    public function SetFormBackId($pId) {
        $this->mFormBackId = $pId;
    }

    public function SetFormOnSubmit($pValue) {
        $this->mFormOnSubmit = $pValue;
    }

    public function SetMultiForm($pValue) {
        $this->mFormMulti = $pValue;
    }

    public function SetFormMethod($pMethod) {
        $this->mFormMethod = $pMethod;
    }

    public function SetCurrentId($pValue) {
        $this->mCurrentPKValue = $pValue;
    }

    public function SetFieldSupport($pName, $pValue) {
        $this->mFieldSupport[$pName] = $pValue;
    }

    private function IsFormfIdExist() {
        //Untuk mencari apakah form fId(form wajib) ada
        //Khusus untuk NON-MULTIFORM
        if (!$this->mFormMulti) {
            foreach ($this->mInputObject as $tIndex => $tRes) {
                if ($tRes["InputName"] == "fId") {
                    return true;
                }
            }
        }
        return false;
    }

    public function PrintForm() {
        $tOutput;
        $tErr = false; //ERROR FLAG
        $tEncType = '';


        //ERROR HANDLER
        //Periksa Form Action
        if (empty($this->mFormName)) {
            //ERROR: Nama belum di set.
            $tErr = true;
            if (CORE_DEVELOPING_MODE)
                $tErrMsg = "Form name is not defined";
            else
                $tErrMsg = "<div class=\"Title\">Form error!</div><div class=\"Content\">Please try again. If the problem still occurred, please contact system developer.</div>";
        }
        if (empty($this->mFormAction)) {
            //ERROR: Action belum di set.
            $tErr = true;
            if (CORE_DEVELOPING_MODE)
                $tErrMsg = "<div class=\"debug\">Form action is not defined. Masalah yang umum terjadi: <ul><li>Nilai action form berasal dari value referer, hal ini disebabkan direct langsung ke url tertentu.<br />Solusi: Jangan melakukan direct url terhadap halaman ini, beri error handler.</li></div>";
            else
                $tErrMsg = "<div class=\"Title\">Form error!</div><div class=\"Title\">Please try again. If the problem still occurred, please contact application developer.</div>";
        }


        $tJml = count($this->mInputObject);

        //LAKUKAN: print form jika Input Object defined + ShowForm = true ($this->mVisibilityForm) + Tidak ada Error
        if (!empty($this->mInputObject) && $this->mVisibilityForm && !$tErr) {
            $isShow = false;

            //CHECK: Apakah diperbolehkan multiple input
            ($this->mFormMulti ? $tMaxMultiForm = TEMPLATE_SETTING_MAX_MULTIPLE_INPUT : $tMaxMultiForm = 1);

            for ($tMulti = 1; $tMulti <= $tMaxMultiForm; $tMulti++) {
                $i = 0;
                foreach ($this->mInputObject as $tIndex => $tRes) {
                    //Nama asli inputfield dibero flag No., KHUSUS UNTUK MULTIPLE INPUT
                    if ($this->mFormMulti)
                        $tRes["InputName"] = $tRes["InputName"] . '_' . $tMulti;

                    $isValidation = false;
                    $tIndex = $tIndex + 1 + ($tJml * ($tMulti - 1));
                    $tType = $tRes["InputType"];
                    $tName = $tRes["InputName"];
                    //KONFIGURASI ID jika tidak diset maka ID = NAME
                    (!isset($tRes["InputId"]) || empty($tRes["InputId"]) ) ? $tId = $tName : $tId = $tRes["InputId"];
                    $tHelper = $tRes["InputHelper"];
                    $tClass = 'formInputField ' . $tRes["InputClass"];
                    $tAddition = $tRes["InputAddition"];
                    $tDisabled = $tRes["InputDisabled"];
                    $tMaxLength = $tRes["SetMaxLength"];
                    $tStyle = $tRes["InputStyle"];
                    $tInfo = $tRes["InputInfo"];
                    $tRequired = $tRes["InputRequired"];
                    $tValidation = $tRes["InputValidation"];

                    //CLASS
                    $tRowClass = $tRes["InputRowClass"];
                    $tHelperButton = '';
                    $tHelperContainer = '';
                    $tInfoOutput = '';

                    //AJAX DEFINE
                    if (isset($tRes["IsAjax"])) {
                        $tIsAjax = $tRes["IsAjax"];
                        $tAjaxTargetId = $tRes["AjaxTargetId"];
                        $tAjaxTargetFile = $tRes["AjaxTargetFile"];
                        $tAjaxFunction = $tRes["AjaxFunction"];
                        $tAjaxEvent = $tRes["AjaxEvent"];
                        $tAjaxEditValue = $tRes["AjaxEditValue"];
                    } else {
                        $tIsAjax = false;
                    }



                    /* SETUP VALUE - FORM FIELD // RECOVERY FORM ROLLED BACK
                     * * Recovery nilai form yang diakibatkan proses gagal. (Rollback action)		
                     */
                    //CHECK: jika terdapat input field selain hidden maka tampilkan button simpan
                    if ($tType != "hidden")
                        $isShow = true;

                    //CHECK: jika nilai value diset, maka rollback tidak berlaku 
                    if ($tRes['InputValue']) {
                        $tValue = $tRes['InputValue'];
                    } else if ($_POST) {
                        //Periksa jika terdapat nilai yang sama dengan nama field maka lakukan set value
                        $tValue = $_POST[$tName];
                    }
                    //KONFIGURASI MAXLENGTH: berlaku hanya untuk TEXT dan TEXT AREA

                    if (!empty($tMaxLength))
                        $tMaxLength = ' maxlength= "' . $tMaxLength . '" ';
                    //KONFIGURASI STYLE
                    if (!empty($tStyle))
                        $tStyle = ' style= "' . $tStyle . '" ';
                    //KONFIGURASI FRONT-END VALIDATION: JS
                    if (!empty($tRes["InputJSValidation"])) {
                        $tJSValidatorMethod = $tRes["InputJSValidation"];
                        $tJSSubmitValidatorMethod = $tRes("InputJSSubmitValidation");
                        $isValidation = true;

                        $tRes["InputAddition"].= ' onfocus= "showInfo(\'' . $tId . '\');"';
                        $tRes["InputAddition"].= ' onblur= "' . $tJSValidatorMethod . '(this, \'' . $tId . '\');"';

                        $tValidator = implode(' ', $tVal);
                        $tSubmitValidator = 'onsubmit= "return ' . $tJSSubmitValidatorMethod . '" ';
                    } else {
                        $tValidator = '';
                        $tOnSubmit = '';
                    }

//KONFIGURASI FRONT_END AJAX:JS
                    if ($tIsAjax) {
                        if (!empty($tAjaxFunction) && !empty($tAjaxTargetId) && !empty($tAjaxTargetFile) && !empty($tAjaxFunction) && !empty($tAjaxEvent)) {
                            $tRes["InputAddition"].= " " . $tAjaxEvent . " = \"" . $tAjaxFunction . "('" . $tAjaxTargetFile . "', '" . ($this->mFormMulti ? $tAjaxTargetId . '_' . $tMulti : $tAjaxTargetId) . "', this.value" . ($tAjaxEditValue? ", ".$tAjaxEditValue:"") . ")\" ";
                        } else {
//ERROR HANDLER
                            echo "<b>ERROR AJAX IMPLEMENTATION " . ($tMaxMultiForm > 1 ? "[MF:" . $tMulti . "]" : "") . ":</b>";
                            if (empty($tAjaxFunction))
                                echo 'Undefined <b>Ajax Function</b>.<br>';
                            if (empty($tAjaxTargetId))
                                echo 'Undefined <b>Ajax Target ID</b>.<br>';
                            if (empty($tAjaxTargetFile))
                                echo 'Undefined <b>Ajax Target File</b>.<br>';
                            if (empty($tAjaxEvent))
                                echo 'Undefined <b>Ajax Event Handler</b>.<br>';
                        }
                    }

                    if ($tValidation)
                        $tAddition = $tValidation;

                    $tFormInput = $this->GenerateInputForm($tRes, $tIndex);

                    if (isset($tRes["FormTitle"]) && !empty($tRes["FormTitle"])) {
                        $tRow = "<tr><td colspan=\"2\"><h3 class=\"form-title\">" . $tRes["FormTitle"] . "</h3></td></div>\n";
                    } else {
//SET CAPTION FORM
                        if (!isset($tRes["InputCaption"]) || empty($tRes["InputCaption"]))
                            $tRes["InputCaption"] = 'unnamed';

//SET VALIDATOR
                        $tInfo = array();
                        if ($tRequired)
                            $tInfo[0] = "<b style=\"color:red\">Required</b>";
                        if (is_int($tMaxLength) && $tMaxLength > 0)
                            $tInfo[1] = "Max Char: " . $tMaxLength . "";
//SET HELPER
                        if (!empty($tHelper)) {
                            $tHelperButton = "\<span id=\"Help-" . $tId . "\" class=\"Help Button\" rel=\"" . $tId . "\"><img src=\"" . TEMPLATE_IMAGES_PATH . "question.png\" style=\"vertical-align:top\"></span>";
                            $tHelperContainer = "\n\t\t<div id=\"Helper-Container-" . $tId . "\" class=\"Helper-Container\" rel=\"" . $tId . "\">\n" .
                                    "\t\t<div align=\"right\"> \n" .
                                    "\t\t\t<h1>" . TEMPLATE_SETTING_FORM_HELP_TITLE . "</h1> \n" .
                                    "\t\t\t<img src=\"" . TEMPLATE_IMAGES_PATH . "helper-close.png\" width=\"15px\" class=\"Button HelperClose\">\n" .
                                    "\t\t</div>\n" .
                                    "\t\t" . $tHelper . "\n" .
                                    "\t\t</div>\n";
                        }

                        foreach ($tInfo as $temp) {
                            $tInfoOutput.= $temp;
                        }


//CHECK: Jika input type adalah hidden maka tidak perlu dibuatkan form dalam table
                        if ($tType == "hidden") {
                            $tRow.= "\t " . $tFormInput . "\n";
                        } else if ($tType == "title") {
                            if ($tMulti == 1)
                                $tRow.= "<div id=\"Template-row-" . $tId . "\" class=\"Template-Form-Container MultiForm First " . $tRowClass . "\" index=\"" . $tMulti . "\"> \n";
                            else {
//BERI TANDA top UNTUK INPUT FIELD PERTAMA, (border top)
                                if ($i == 0)
                                    $tRow.= "<div id=\"Template-row-" . $tId . "\" class=\"Template-Form-Container MultiForm top " . $tRowClass . "\" index=\"" . $tMulti . "\"> \n";
                                else
                                    $tRow.= "<div id=\"Template-row-" . $tId . "\" class=\"Template-Form-Container MultiForm " . $tRowClass . "\" index=\"" . $tMulti . "\"> \n";
                            }
                            $tRow.= $tFormInput . "\n";
                            $tRow.= "</div>\n";
                        }else if ($tType == "caption") {
//KHUSUS UNTUK FORM PERTAMA TIDAK DIHIDE
                            if ($tMulti == 1)
                                $tRow.= "<div id=\"Template-row-" . $tId . "\" class=\"Template-Form-Container MultiForm First " . $tRowClass . "\" index=\"" . $tMulti . "\"> \n";
                            else {
//BERI TANDA top UNTUK INPUT FIELD PERTAMA, (border top)
                                if ($i == 0)
                                    $tRow.= "<div id=\"Template-row-" . $tId . "\" class=\"Template-Form-Container MultiForm top " . $tRowClass . "\" index=\"" . $tMulti . "\"> \n";
                                else
                                    $tRow.= "<div id=\"Template-row-" . $tId . "\" class=\"Template-Form-Container MultiForm " . $tRowClass . "\" index=\"" . $tMulti . "\"> \n";
                            }
                            $tRow.= "<ul>\n";
                            $tRow.= "\t <li class=\"Left\">" . $tRes["InputCaption"] . "</li> \n";

//Berikan Flag First untuk input field pertama, digunakan untuk keperluan focus selecto multi form
                            if ($i == 0)
                                $isFirst = " First";
                            else
                                $isFirst = "";

                            $tRow.= "\t <li class=\"Right" . $isFirst . "\" id=\"" . $tId . "-Container\">" . $tFormInput . " <span id=\"BackspaceButton-" . $tId . "\" class=\"BackspaceButton\" rel=\"" . $tId . "\"><img src=\"" . TEMPLATE_IMAGES_PATH . "backspace.png\" style=\"vertical-align:middle\"></span></li>\n";
                            $tRow.= "</ul>\n";
                            $tRow.= "</div>\n";

//BUTTON CREATE FORM
//Ditaruh pada akhir row tiap form, kecuali form terakhir
                            if (($i + 1) == $tJml && $tMulti < $tMaxMultiForm) {
                                if ($tMulti == 1)
                                    $tRow.= "<div class=\"MultiForm First\" index=\"" . $tMulti . "\"><input type=\"button\" class=\"button MultiForm\" index=\"" . $tMulti . "\" value=\"Tambah Form\" /></div>\n\n";
                                else
                                    $tRow.= "<div class=\"MultiForm\" index=\"" . $tMulti . "\"><input type=\"button\" class=\"button MultiForm\" index=\"" . $tMulti . "\" value=\"Tambah Form\" /></div>\n\n";
                            }
                        }
                        else if ($tType == "checkbox") {
//KHUSUS UNTUK FORM PERTAMA TIDAK DIHIDE
                            if ($tMulti == 1)
                                $tRow.= "<div id=\"Template-row-" . $tId . "\" class=\"Template-Form-Container MultiForm First " . $tRowClass . "\" index=\"" . $tMulti . "\"> \n";
                            else {
//BERI TANDA top UNTUK INPUT FIELD PERTAMA, (border top)
                                if ($i == 0)
                                    $tRow.= "<div id=\"Template-row-" . $tId . "\" class=\"Template-Form-Container MultiForm top " . $tRowClass . "\" index=\"" . $tMulti . "\"> \n";
                                else
                                    $tRow.= "<div id=\"Template-row-" . $tId . "\" class=\"Template-Form-Container MultiForm " . $tRowClass . "\" index=\"" . $tMulti . "\"> \n";
                            }
                            $tRow.= "<ul>\n";
                            $tRow.= "\t <li class=\"Left\" style=\"text-align:center\">" . $tFormInput . "</li> \n";

//Berikan Flag First untuk input field pertama, digunakan untuk keperluan focus selecto multi form
                            if ($i == 0)
                                $isFirst = " First";
                            else
                                $isFirst = "";

                            $tRow.= "\t <li class=\"Right" . $isFirst . "\" id=\"" . $tId . "-Container\">" . $tRes["InputCaption"] . " </li>\n";
                            $tRow.= "</ul>\n";
                            $tRow.= "</div>\n";

//BUTTON CREATE FORM
//Ditaruh pada akhir row tiap form, kecuali form terakhir
                            if (($i + 1) == $tJml && $tMulti < $tMaxMultiForm) {
                                if ($tMulti == 1)
                                    $tRow.= "<div class=\"MultiForm First\" index=\"" . $tMulti . "\"><input type=\"button\" class=\"button MultiForm\" index=\"" . $tMulti . "\" value=\"Tambah Form\" /></div>\n\n";
                                else
                                    $tRow.= "<div class=\"MultiForm\" index=\"" . $tMulti . "\"><input type=\"button\" class=\"button MultiForm\" index=\"" . $tMulti . "\" value=\"Tambah Form\" /></div>\n\n";
                            }
                        } else {
//SET VALIDATOR EVENT HANDLER
                            if ($tMulti == 1)
                                $tRowValidator = "<div id=\"" . $tId . "-Info\" class=\"FormInfo-Container MultiForm First " . $tRowClass . "\" index=\"" . $tMulti . "\">\n";
                            else
                                $tRowValidator = "<div id=\"" . $tId . "-Info\" class=\"FormInfo-Container MultiForm " . $tRowClass . "\" index=\"" . $tMulti . "\">\n";

                            $tRowValidator.= "<ul>\n";
                            $tRowValidator.= "	<li class=\"Left\"></li> \n";
                            $tRowValidator.= "	<li class=\"Right\"> \n" .
                                    "	\t <div class=\"FormInfo-Helper\">" . $tHelperButton . "</div>" .
                                    "	\t <div class=\"FormInfo-Info\">" . $tInfoOutput . "</div> \n " .
                                    "	\t <div class=\"FormInfo-Alert\"></div> \n" .
                                    $tHelperContainer .
                                    "	</li> \n";
                            $tRowValidator.= "</ul>\n";

                            $tRowValidator.= "</div>\n";

//KHUSUS UNTUK FORM PERTAMA TIDAK DIHIDE
                            if ($tMulti == 1)
                                $tRow.= "\n<div id=\"Template-row-" . $tId . "\" class=\"Template-Form-Container MultiForm First " . $tRowClass . "\" index=\"" . $tMulti . "\"> \n";
                            else {
//BERI TANDA top UNTUK INPUT FIELD PERTAMA, (border top)
                                if ($i == 0)
                                    $tRow.= "<div id=\"Template-row-" . $tId . "\" class=\"Template-Form-Container MultiForm top " . $tRowClass . "\" index=\"" . $tMulti . "\"> \n";
                                else
                                    $tRow.= "<div id=\"Template-row-" . $tId . "\" class=\"Template-Form-Container MultiForm " . $tRowClass . "\" index=\"" . $tMulti . "\"> \n";
                            }

// Berikan CLASS khusus untuk input form TEXTAREA
                            if ($tType == "textarea")
                                $tTextareaClass = " Textarea ";
                            else
                                $tTextareaClass = "";

//Berikan Flag First untuk input field pertama, digunakan untuk keperluan focus selecto multi form
                            if ($i == 0)
                                $isFirst = " First";
                            else
                                $isFirst = "";


                            $tRow.= "\t<ul class=\"" . $tTextareaClass . "\" >\n";
                            $tRow.= "\t\t<li class=\"Left \">" . $tRes["InputCaption"] . "</li> \n";

                            $tRow.= "\t\t<li class=\"Right Template-Form-Input" . $isFirst . "\" id=\"" . $tId . "-Container\">" . 
                                    $tFormInput . ($tType != "button" ? " \t<span id=\"BackspaceButton-" . $tId . "\" class=\"BackspaceButton\" rel=\"" . $tId . "\"><img src=\"" . TEMPLATE_IMAGES_PATH . "backspace.png\" style=\"vertical-align:middle\"></span>\n\t\t" : "") . "</li>\n";
                            $tRow.= "\t</ul>\n";
                            $tRow.= "</div>\n\n";

                            if (isset($tRowValidator) && !empty($tRowValidator)) {
                                $tRow.= $tRowValidator;
                            }

//BUTTON CREATE FORM
//Ditaruh pada akhir row tiap form, kecuali form terakhir
                            if (($i + 1) == $tJml && $tMulti < $tMaxMultiForm) {
                                if ($tMulti == 1)
                                    $tRow.= "<div class=\"MultiForm First\" index=\"" . $tMulti . "\"><input type=\"button\" class=\"button MultiForm\" index=\"" . $tMulti . "\" value=\"Tambah Form\" /></div>\n\n";
                                else
                                    $tRow.= "<div class=\"MultiForm\" index=\"" . $tMulti . "\"><input type=\"button\" class=\"button MultiForm\" index=\"" . $tMulti . "\" value=\"Tambah Form\" /></div>\n\n";
                            }
                        }
                    }
                    $i++;
                }
            }

//SUBMIT BUTTON
            if ($isShow && $this->mFormIsSubmitButton)
                $tRow.= "<div class=\"Template-Form-Container Submit\"><input type=\"button\" class=\"button submit\" value=\"" . TEMPLATE_SETTING_FORM_SUBMIT_BUTTON_CAPTION_DEFAULT . "\" /> <input type=\"button\" class=\"button back\" value=\"" . TEMPLATE_SETTING_FORM_BACK_BUTTON_CAPTION_DEFAULT . "\" href=\"" . $this->mFormTargetBack . "\" rel=\"" . $this->mFormBackId . "\" /></div>";


//FORM HEADER
            if (strpos($_SERVER['REQUEST_URI'], "?")) {
                $temp = explode("?", $_SERVER['REQUEST_URI']);
                $tRef = "main.php?" . $temp[1];
            }
            $tFormHeader = "\n<form name=\"" . $this->mFormName . "\" method=\"" . $this->mFormMethod . "\" action=\"" . $this->mFormAction . "\" " . $tEncType . " " . $tSubmitValidator . " class=\"Template-Form\" " . ($this->mFormOnSubmit ? "onsubmit=\"" . $this->mFormOnSubmit . "\"" : "") . ">\n";
            $tFormHeader.= "\t <input type=\"hidden\" name=\"HTTP_REFERER\" value=\"" . $tRef . "\" /> \n";
            $tFormHeader.= "\t <input type=\"hidden\" name=\"fAct\" value=\"" . $this->mAction . "\" /> \n";
            $tFormHeader.= "\t <input type=\"hidden\" name=\"fCurrentPKValue\" value=\"" . $this->mCurrentPKValue . "\" /> \n";
            $tFormHeader.= "\t <input type=\"hidden\" name=\"fBackId\" value=\"" . $this->mFormBackId . "\" /> \n";

            //UNTUK FORM MULTI: dibutuhkan satu buah input field bernama fId untuk keperluan back link
            if ($this->mFormMulti)
                $tFormHeader.= "\t <input type=\"hidden\" name=\"fId\" id=\"fId\" value=\"\" /> \n";
            else {
                if (!$this->IsFormfIdExist()) {
                    $tFormHeader.= "\t <input type=\"hidden\" name=\"fId\" id=\"fId\" value=\"\" /> \n";
                }
            }
            //FIELD SUPPORT. variable2 hidden yang menjadi nilai support, biasanya PK yang tidak digunakan untuk proeses manipulasi database
            if (count($this->mFieldSupport) > 0) {
                foreach ($this->mFieldSupport as $tKey => $tVal) {
                    $tFormHeader.= "\t <input type=\"hidden\" name=\"" . $tKey . "\" value=\"" . $tVal . "\" />\n";
                }
            }

            $tFormHeader.= "\t <input type=\"hidden\" name=\"isMultiForm\" value=\"" . $this->mFormMulti . "\" /> \n";
            $tFormHeader.= "\t <input type=\"hidden\" name=\"fMultiFormCount\" id=\"fMultiFormCount\" value=\"1\" /> \n";
            $tFormFooter = "</form>\n";

//PRINT;
            echo "\n<div id=\"Template-Form\">\n\t<div><h1 class=\"Template-Form\">" . $this->mFormTitle . "</h1></div>\n";
            if (!empty($_POST[ERROR_SECTION]) && !empty($_POST[ERROR_CODE])) {
                $tErrorMsg = new ErrorMessage();
                $tErrorMsg->SetErrorCode($_POST[ERROR_CODE]);
                $tErrorMsg->SetErrorSection($_POST[ERROR_SECTION]);
                echo "<div class=\"ErrorMessage-Container\">" . $tErrorMsg->GetErrorMessage() . "</div>";
            }
            //echo $tFormHeader . $tRow . $tFormFooter . "</div>";
            echo $tFormHeader . "\n\t <div class=\"" . $this->mFormClass . "\">\n" . $tRow . "\n\t </div>\n" . $tFormFooter;
            echo "\n</div>\n";
        } else if ($tErr) {
            if (CORE_DEVELOPING_MODE)
                echo "<div class=\"Debug\">$tErrMsg</div>";
            else
                echo "<div class=\"ErrorMessage-Container\">$tErrMsg</div>";
        } else if (!$this->mVisibilityForm) {
            echo "<div class=\"ErrorMessage-Container\"><div class=\"Title\">Terjadi Kesalahan</div><div class=\"Content\">$this->mErrorHandlerMessage</div></div>";
        }
    }

    private function GenerateInputForm($pRes, $pIndex) {
        $tType = $pRes["InputType"];
        $tName = $pRes["InputName"];
        $tClass = 'formInputField ' . $pRes["InputClass"];
        $tAddition = $pRes["InputAddition"];
        $tDisabled = $pRes["InputDisabled"];
        $tMaxLength = $pRes["SetMaxLength"];
        $tStyle = $pRes["InputStyle"];
        $tInfo = $pRes["InputInfo"];
        $tRequired = $pRes["InputRequired"];
        $tValidation = $pRes["InputValidation"];
        $tValue = (isset($pRes['InputValue']) ? $pRes['InputValue'] : $_POST[$tName]);
        $tIndex = ($pIndex ? $pIndex : "");
        //echo $tName.'<br>'.($tRequired?'true':'false').'<hr>';
        //MENENTUKAN TABINDEX: index - jml hidden field
        if ($tType == 'hidden')
            $this->mHiddentFieldCount++;
        $tIndex = $tIndex - $this->mHiddentFieldCount;

        (!isset($pRes["InputId"]) || empty($pRes["InputId"]) ) ? $tId = $tName : $tId = $pRes["InputId"];

        switch ($tType) {
            case 'button' : {
                    $tFormInput = '<input type="' . $tType . '" name="' . $tName . '" id="' . $tId . '" value="' . ($tValue != "" ? $tValue : $_POST['EDIT_' . $tName]) . '" ' .
                            ' class="' . $tClass . '" ' . $tAddition . ' ' . $tDisabled . ' ' . $tMaxLength . ' tabindex="' . $tIndex . '" />';
                    break;
                }
            case 'title' : {
                    $tFormInput = '<div type="' . $tType . '" tabindex="' . $tIndex . '" class="' . $tClass . '" ' . ($tId ? "id=\"" . $tId . "\"" : "") . '><h3>' . $tValue . '</h3></div>';
                    break;
                }
            case 'caption' : {
                    $tFormInput = '<div type="' . $tType . '" tabindex="' . $tIndex . '" class="' . $tClass . '" ' . ($tId ? "id=\"" . $tId . "\"" : "") . '>' . $tValue . '</div>';
                    break;
                }
            case 'hidden' : {
                    $tFormInput = '<input type="' . $tType . '" name="' . $tName . '" id="' . $tId . '" value="' . ($tValue != "" ? $tValue : $_POST['EDIT_' . $tName]) . '" />';
                    break;
                }
            case 'checkbox' : {
                    $tFormInput = '<input type="' . $tType . '" name="' . $tName . '" id="' . $tId . '" value="' . ($tValue != "" ? $tValue : $_POST['EDIT_' . $tName]) . '" ' .
                            ' class="' . $tClass . '" ' . $tAddition . ' ' . $tDisabled . ' ' . $tMaxLength . ' ' . $tValidator . ' tabindex="' . $tIndex . '" />';
                }
            case 'text' : {
                    $tFormInput = '<input type="' . $tType . '" name="' . $tName . '" id="' . $tId . '" value="' . ($tValue != "" ? $tValue : $_POST['EDIT_' . $tName]) . '" ' .
                            ' class="' . $tClass . '" ' . $tAddition . ' ' . $tDisabled . ' ' . $tMaxLength . ' ' . $tValidator . ' tabindex="' . $tIndex . '"  ' . ($tRequired ? 'rel="required"' : 'req') . ' />';
                    //CHECK: apakah autocomplete(CLASS: Autocomplete)
                    if (strpos($tClass, "Autocomplete")) {
                        $tFormInput.= "<div class=\"" . $tName . " Autocomplete-Alt\"></div>";
                    }
                    break;
                }

            case 'textarea' : {
                    $tFormInput = '<textarea name="' . $tName . '" id="' . $tId . '" class="' . $tClass . '" ' . $tAddition . ' 
				  ' . $tDisabled . ' ' . $tValidator . ' tabindex="' . $tIndex . '" />' . ($tValue != "" ? $tValue : $_POST['EDIT_' . $tName]) . ' </textarea>';
                    break;
                }

            case 'file' : {
                    $tFormInput = '<input type="' . $tType . '" name="' . $tName . '" id="' . $tId . '" value="' . $tValue . '" ' .
                            ' class="' . $tClass . '" ' . $tAddition . ' ' . $tDisabled . ' ' . $tMaxLength . ' ' . $tValidator . ' tabindex="' . $tIndex . '" />';
                    $tEncType = ' enctype="multipart/form-data" ';
                    break;
                }

            case 'password' : {
                    $tFormInput = '<input type="' . $tType . '" name="' . $tName . '" id="' . $tId . '" value="' . $tValue . '" ' .
                            'class="' . $tClass . '" ' . $tAddition . ' ' . $tDisabled . ' ' . $tMaxLength . ' ' . $tValidator . ' tabindex="' . $tIndex . '" ' . ($tRequired ? 'rel="required"' : 'req') . ' />';
                    break;
                }

            case 'select' : {
                    $tFormInput = "\n\t<select name=\"" . $tName . "\" id=\"" . $tId . "\" class=\"" . $tClass . "\" " . $tAddition . " " .
                            $tDisabled . " " . $tValidator . " tabindex=\"" . $tIndex . "\" > \n";

                    if ($pRes[InputOption]) {
                        foreach ($pRes[InputOption] as $tEl) {
                            if ($tValue === $tEl[Value])
                                $tFormInput.= "\t\t<option value=\"" . $tEl[Value] . "\" selected>" . $tEl[Caption] . "</option> \n";
                            else if ($_POST['EDIT_' . $tName] == $tEl[Value])
                                $tFormInput.= "\t\t<option value=\"" . $tEl[Value] . "\" selected>" . $tEl[Caption] . "</option> \n";
                            else
                                $tFormInput.= "\t\t<option value=\"" . $tEl[Value] . "\" >" . $tEl[Caption] . "</option> \n";
                        }
                    }
                    else {
                        if ($pRes[InputOptionNullDataCaption])
                            $tFormInput.= "\t\t<option value=\"\" >" . $pRes[InputOptionNullDataCaption] . "</option> \n";
                        else
                            $tFormInput.= "\t\t<option value=\"\" >-- belum diset --</option> \n";
                    }
                    $tFormInput.= "\t</select> \n";
                    break;
                }
        }

        return $tFormInput;
    }

    public function SetType($pParams, $pIndex = null) {
        if (!is_int($pIndex))
            $pIndex = $this->mFormInputPointer;
        $this->mInputObject[$pIndex]['InputType'] = $pParams;
    }

    public function SetTitle($pParams, $pIndex = null) {
        if (!is_int($pIndex))
            $pIndex = $this->mFormInputPointer;
        $this->mInputObject[$pIndex]['FormTitle'] = $pParams;
    }

    public function SetName($pParams, $pIndex = null) {
        if (!is_int($pIndex))
            $pIndex = $this->mFormInputPointer;
        $this->mInputObject[$pIndex]['InputName'] = $pParams;
    }

    public function SetId($pParams, $pIndex = null) {
        if (!is_int($pIndex))
            $pIndex = $this->mFormInputPointer;
        $this->mInputObject[$pIndex]['InputId'] = $pParams;
    }

    public function SetOptionValueFromDataArray($pParams, $pIndex = null) {
        if (!is_int($pIndex))
            $pIndex = $this->mFormInputPointer;

        if (is_array($pParams)) {
            $i = 0;
            $this->mInputObject[$pIndex][InputOption][$i][Value] = "";

            //CHECK: DEFAULT NULL VALUE CAPTION
            if ($this->mInputObject[$pIndex][InputOptionDefaultNullValueCaption])
                $this->mInputObject[$pIndex][InputOption][$i][Caption] = $this->mInputObject[$pIndex][InputOptionDefaultNullValueCaption];
            else
                $this->mInputObject[$pIndex][InputOption][$i][Caption] = "-- Pilih --";

            $i++;
            foreach ($pParams as $tRes) {
                $this->mInputObject[$pIndex][InputOption][$i][Value] = $tRes[0];
                $this->mInputObject[$pIndex][InputOption][$i][Caption] = $tRes[1];
                $i++;
            }
        } else {
//ERROR: DATA TYPE TIDAK SESUAI DENGAN YANG DIMINTA
        }
    }

    public function SetOptionValueFromDataTable($pParams, $pIndex = null) {
        if (!is_int($pIndex))
            $pIndex = $this->mFormInputPointer;

        if (is_object($pParams)) {
            $i = 0;

            //CHECK: Apakah data ada?
            if ($pParams->Count() > 0) {
                //CHECK: DEFAULT NULL VALUE CAPTION
                if ($this->mInputObject[$pIndex][InputOptionDefaultNullValueCaption])
                    $this->mInputObject[$pIndex][InputOption][$i][Caption] = $this->mInputObject[$pIndex][InputOptionDefaultNullValueCaption];
                else
                    $this->mInputObject[$pIndex][InputOption][$i][Caption] = "-- Pilih --";

                $i++;
                while ($pParams->MoveNext()) {
                    $this->mInputObject[$pIndex][InputOption][$i][Value] = $pParams->id;
                    $this->mInputObject[$pIndex][InputOption][$i][Caption] = $pParams->value;
                    $i++;
                }
            } else {
                $this->mInputObject[$pIndex][InputOption][$i][Caption] = "-- Data Tidak Ada --";
            }
        } else {
//ERROR: DATA TYPE TIDAK SESUAI DENGAN YANG DIMINTA
        }
        // $pParams->MoveFirst();
        // $pParams->MovePrevious();
    }

    public function SetOptionNullDataCaption($pValue, $pIndex = null) {
        if (!is_int($pIndex))
            $pIndex = $this->mFormInputPointer;
        $this->mInputObject[$pIndex][InputOptionNullDataCaption] = $pValue;
    }

    public function SetOptionDefaultNullValueCaption($pValue, $pIndex = null) {
        if (!is_int($pIndex))
            $pIndex = $this->mFormInputPointer;
        $this->mInputObject[$pIndex][InputOptionDefaultNullValueCaption] = $pValue;
    }

    public function SetCaption($pParams, $pIndex = null) {
        if (!is_int($pIndex))
            $pIndex = $this->mFormInputPointer;
        $this->mInputObject[$pIndex]['InputCaption'] = $pParams;
    }

    public function SetMaxLength($pParams, $pIndex = null) {
        if (!is_int($pIndex))
            $pIndex = $this->mFormInputPointer;
//validasiInputType();
        $this->mInputObject[$pIndex]['SetMaxLength'] = $pParams;
    }

    public function SetValue($pParams, $pIndex = null) {
        if (!is_int($pIndex))
            $pIndex = $this->mFormInputPointer;
        $this->mInputObject[$pIndex]['InputValue'] = $pParams;
    }

    public function SetClass($pParams, $pIndex = null) {
        if (!is_int($pIndex))
            $pIndex = $this->mFormInputPointer;
        $this->mInputObject[$pIndex]['InputClass'] = $pParams;
    }

    public function SetStyle($pParams, $pIndex = null) {
        if (!is_int($pIndex))
            $pIndex = $this->mFormInputPointer;
        $this->mInputObject[$pIndex]['InputStyle'] = $pParams;
    }

    public function SetAjax($pParams, $pIndex = null) {
//pFormDepender: form ybs menjadi acuan pada form target.
// menggunakan metode ajax sehingga nilai form target bergantung pada nilai form acuan.
//SET INPUT INDEX
        if (!is_int($pIndex))
            $pIndex = $this->mFormInputPointer;

        if (is_array($pParams)) {
            $this->mInputObject[$pIndex]['IsAjax'] = true;
            $this->mInputObject[$pIndex]['AjaxTargetId'] = $pParams['TargetId'];
            $this->mInputObject[$pIndex]['AjaxTargetFile'] = $pParams['TargetFile'];
            $this->mInputObject[$pIndex]['AjaxFunction'] = $pParams['Function'];
            $this->mInputObject[$pIndex]['AjaxEvent'] = $pParams['Event'];
            $this->mInputObject[$pIndex]['AjaxEditValue'] = $pParams['EditValue'];
        } else {
//ERROR: HRS ARRAY
        }
    }

    public function SetJSValidation($pParams, $pIndex = null) {
        if (!is_int($pIndex))
            $pIndex = $this->mFormInputPointer;
        $this->mInputObject[$pIndex]['InputJSValidation'] = $pParams;
    }

    public function SetJSSubmitValidation($pParams, $pIndex = null) {
        if (!is_int($pIndex))
            $pIndex = $this->mFormInputPointer;
        $this->mInputObject[$pIndex]['InputJSSubmitValidation'] = $pParams;
    }

    public function SetIsRequired($pParams, $pIndex = null) {
        if (!is_int($pIndex))
            $pIndex = $this->mFormInputPointer;
        $this->mInputObject[$pIndex]['InputRequired'] = $pParams;
    }

    public function SetIsDisabled($pParams, $pIndex = null) {
        if (!is_int($pIndex))
            $pIndex = $this->mFormInputPointer;
        $this->mInputObject[$pIndex]['InputDisabled'] = $pParams;
    }

    public function SetAddition($pParams, $pIndex = null) {
        if (!is_int($pIndex))
            $pIndex = $this->mFormInputPointer;
        $this->mInputObject[$pIndex]['InputAddition'] = $pParams;
    }

    public function SetHelper($pParams, $pIndex = null) {
        if (!is_int($pIndex))
            $pIndex = $this->mFormInputPointer;
        $this->mInputObject[$pIndex]['InputHelper'] = $pParams;
    }

    public function SetInfo($pParams, $pIndex = null) {
        if (!is_int($pIndex))
            $pIndex = $this->mFormInputPointer;
        $this->mInputObject[$pIndex]['InputInfo'] = $pParams;
    }

    public function SetRowClass($pClassName, $pIndex = null) {
        if (!is_int($pIndex))
            $pIndex = $this->mFormInputPointer;
        $this->mInputObject[$pIndex]['InputRowClass'] = $pClassName;
    }

    public function setInputForm($pParams = null) {
        if (!is_int($pIndex))
            $pIndex = $this->mFormInputPointer;
        $this->mInputObject[$tIndex]['fieldName'] = $pParams['fieldName'];
        $this->mInputObject[$tIndex]['inputName'] = $pParams['inputName'];
        $this->mInputObject[$tIndex]['inputName'] = $pParams['inputName'];
    }

    public function SetIsSubmitButton($pValue) {
        $this->mFormIsSubmitButton = $pValue;
    }

    public function SetVisibilityForm($pVisiblity, $pErrorHandlerMessage) {
        $this->mVisibilityForm = $pValue;
        $this->mErrorHandlerMessage = $pErrorHandlerMessage;
    }

}